package uit.nfcmenu.waiter.activities;

import java.util.ArrayList;

import uit.nfcmenu.models.*;
import uit.nfcmenu.waiter.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

public class GalaryItemsActivity extends Activity implements OnClickListener, IDataChangeHandler  {
	public static String tag = GalaryItemsActivity.class.getSimpleName();

	// hard 8 // TODO dynamic menu-items numbers in vertical-scroll
	private View menu1, menu2, menu3, menu4, menu5, menu6, menu7, menu8;	
	private Button bntOrderList, bntLogout, bntMenu;
	
	private int mainId;
	private DatabaseManager mDBManager;
	


	private final Integer[][] imageDataList = { 
			{R.drawable.menu1, R.drawable.menu2, R.drawable.menu2, R.drawable.menu1},
			{R.drawable.menu2, R.drawable.menu2, R.drawable.menu1, R.drawable.menu3}, 
			{R.drawable.menu1, R.drawable.menu2, R.drawable.menu2, R.drawable.menu1},
			{R.drawable.menu2, R.drawable.menu2, R.drawable.menu1, R.drawable.menu3}, 
			{R.drawable.menu1, R.drawable.menu2, R.drawable.menu2, R.drawable.menu1},
			{R.drawable.menu2, R.drawable.menu2, R.drawable.menu1, R.drawable.menu3}, 
			{R.drawable.menu3, R.drawable.menu3, R.drawable.menu3, R.drawable.menu3}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.galary_items);
//		// init data for views
//		mainId = Integer.parseInt(getIntent().getExtras().getString(MainMenuActivity.tag));
//		Log.i(tag, "get mainId="+mainId);
//		// get sub menu by main and view to the screen
//		mDBManager = DatabaseManager.getInstance(this);
//		ArrayList<MenuModel> menus = mDBManager.getSubMenus(mainId);
		
//		Gallery gallery = (Gallery) findViewById(R.id.my_galary);
//		gallery.setAdapter(new ImageAdapter(this));
//		gallery.setOnItemLongClickListener(longClickListener);
//		gallery.setGravity(Gravity.LEFT);
//		gallery.setPadding(10, 10, 10, 10);
	}
	
	private OnItemLongClickListener longClickListener = new OnItemLongClickListener() {
		public boolean onItemLongClick(AdapterView<?> parent, View v, int position,
				long id) {
			String temp = "Hello World!";
			Toast toast = Toast.makeText(getBaseContext(), temp, Toast.LENGTH_SHORT);
			toast.show();
			return true;
		}
	};

	public class ImageAdapter extends BaseAdapter {
		private LayoutInflater inflater = null;

		public ImageAdapter(Context c) {
			inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			if (imageDataList != null) {
				return imageDataList.length*4;
			} else {
				return 0;
			}
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			Log.e(tag, "pos="+position);
			View view = convertView;
			Holder holder;

			if (view == null) {
				// get layouts
				view = inflater.inflate(R.layout.vertical_linear_render, parent, false);
				holder = new Holder();
				holder.img1 = (ImageView) view.findViewById(R.id.img1);
				holder.img2 = (ImageView) view.findViewById(R.id.img2);
				holder.img3 = (ImageView) view.findViewById(R.id.img3);
				holder.img4 = (ImageView) view.findViewById(R.id.img4);
			} else {
				holder = (Holder) view.getTag();
			}

			// assign contents
			if (position%4 == 0) {
				int d = position / 4;
				try {
					holder.img1.setImageResource(imageDataList[d][0]);
					holder.img2.setImageResource(imageDataList[d][1]);
					holder.img3.setImageResource(imageDataList[d][2]);
					holder.img4.setImageResource(imageDataList[d][3]);
				} catch (ArrayIndexOutOfBoundsException e) { }
			}
			
			return view;
		}
		
		class Holder {
			ImageView img1;
			ImageView img2;
			ImageView img3;
			ImageView img4;
		}
	}

	public void onDataChanged() {
		// TODO Auto-generated method stub
		
	}

	public Context getContext() {
		// TODO Auto-generated method stub
		return getBaseContext();
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	
	// ========================
	
}
